package com.qf.dao.impl;

import com.qf.dao.BookDao;
import com.qf.entity.Page;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.qf.entity.Book;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {
    private QueryRunner runner=new QueryRunner(DBUtil.getDateSource());
    public List<com.qf.entity.Book> selectAll() {
        String sql="select * from book";
        try {
            return runner.query(sql,new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
        String sql = "select * from book limit ?,?";
        try {
            return runner.query(sql,new BeanListHandler<Book>(Book.class),page.getStartRows(),page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllByPageCount() {
        String sql = "select count(*) from book";
        try {
           long count= runner.query(sql,new ScalarHandler<Long>());
           return (int)count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
